#!/usr/bin/env python
# coding=utf-8
# author:wade
# contact: 317909531@qq.com
# datetime:2020/5/4 20:41
import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "shareAPI2020TB.settings")# project_name 项目名称
django.setup()
from shareAPI2020TB.settings import MEDIA_ROOT
import json
import re

from tbitem import models


class ZhengZe():
    def findUrl_Title(self,content):

        FakeURL = re.compile('''POSTRESULT = postDISCUZ_By_FakeUrl\(FakeUrl='(.*?)',pages=5,subject''', re.S)
        FakeURL_value = re.findall(FakeURL, content)
        # print(FakeURL_value[0])
        title = re.compile('''subject="(.*?)",username=''', re.S)
        title_value = re.findall(title, content)
        print(title_value[0])


        return title_value[0],FakeURL_value[0]

def readText():
    jsonLIST =[]
    # 2.按行读取
    textlist=[]


    for line in open('原始url.txt', errors='ignore'):
        # 清洗空格后如果不为空就下一步处理
        if line.strip() != '':
            # print(f"本行内容为{line}")
            # 每行的数据都需要清洗空格后进行下一步处理

            # print(line.strip())
            jsonLIST.append(line.strip())


        else:
            print("本行为空")

    zhengze = ZhengZe()
    for item in jsonLIST:
        title,FakeURL= zhengze.findUrl_Title(item)
        mylist = {'title': '', 'FakeURL': '', 'hasPOst': False}
        mylist['title'] = title
        mylist['FakeURL'] = FakeURL
        mylist['hasPOst'] = False

        textlist.append(mylist)
    print(textlist)
    return textlist

def writejson(dict, jsonname):
    try:
        with open(f"{jsonname}", "w+")as f:  # 写入json文件
            f.write(json.dumps(dict,ensure_ascii=False))
            f.close()
    except Exception as e:
        print(e)
        pass


#读一段
def readjson(jsonname):
    '''
    cookies = readjson('jsonData.json')
    print(cookies[0])
    '''

    with open(os.path.join(MEDIA_ROOT,jsonname), "r")as f:
        cookies = json.loads(f.read())
        # print(cookies)
    return cookies


def storeJsonFile_To_Db():
    jsonData = readjson('jsonData.json')
    for item in jsonData:
        try:
            john = models.Jdong(FakeURL=item['FakeURL'], title=item['title'], hasPOst=False)
            john.save()
            print('插入成功')
        except Exception as e:
            print(e)
            continue



if __name__ == '__main__':

    #这两个代码放入原始url.txt到同一目录，可以转换出来jsonData.json
    # print(readText())
    # writejson(readText(),'jsonData.json')

    #这个可以离线存储数据库内容，把jsonData.json读到数据库中
    storeJsonFile_To_Db()
    print('123')
